<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<script type="text/javascript">
    var roleId;
		$().ready(function(){
			
			$('#newRoleBtn').die('click').live('click',function(){
				var params={
						  'title':'新增角色',
						  'url':$(this).attr('url'),
						  'width':400,
						  'height':280
						 };
				createIframeDialog(params);
			});
			$('#editRoleBtn').die('click').live('click',function(){
				var params={
						  'title':'编辑角色',
						  'url':$(this).attr('url')+"&id="+roleId,
						  'width':400,
						  'height':280,
						  btnSucFunc:function(){
							  getRoleInfoByTree(roleId);
						  }
						 };
				createIframeDialog(params);
			});
			$('#delRoleBtn').die('click').live('click',function(){
				var roleName=$("#"+roleId).find("a:not(ins)").text();
				var msg='确认删除<span style="color:red;font-weight:bold">'+roleName+'</span>角色?';
				createConfirmDialog('确认删除角色?',msg,function(){
					$.ajax({
						type : "GET",
						url : '<%=request.getContextPath()%>/role.do?method=deleteRoleById&id='+roleId,
						dataType : "json",
						success : function(data) {
							if (data['status'] == 'ERROR') {
								createAlertDialog('提示信息','删除角色失败!');
							} else {
								createAlertDialog('提示信息','删除角色成功!',function(){
									window.location.reload();
								});
							}
						},
						error : function() {
							createAlertDialog('提示信息','网络繁忙,请稍后再试!');
						}
					});
				});
			});
			
			$('#addUserToRoleBtn').die('click').live('click',function(){
				var path='<%=request.getContextPath()%>/role.do?method=addUserToRole&id='+roleId+'&userids=';
				var params={
						  'title':'添加用户到角色',
						  'url':$(this).attr('url')+"?id="+roleId,
						  'frameSubmitType':'gridSelect',
						  'path':path,
						  'width':720,
						  'height':470,
						  btnSucFunc:function(){
							  $("#"+dataGridID).trigger("reloadGrid");
						  }
						 };
				createIframeDialog(params);
			});
			
			$('#delUserFromRoleBtn').die('click').live('click',function(){
				var userids; 
				userids = jQuery("#roleUserDataGrid").jqGrid('getGridParam','selarrrow');
				if(userids.length<=0){
					createAlertDialog('提示信息','请选择需要删除的用户!');
					return false;
				}
				var roleName=$("#"+roleId).find("a:not(ins)").text();
				var msg='确认从<span style="color:red;font-weight:bold">'+roleName+'</span>角色中删除id为('+userids+')的用户?';
				var alertMsg='从<span style="color:red;font-weight:bold">'+roleName+'</span>角色中删除id为('+userids+')的用户';
				createConfirmDialog('确认删除角色?',msg,function(){
					$.ajax({
						type : "GET",
						url : '<%=request.getContextPath()%>/role.do?method=deleteUsersFormRoleById&id='+roleId+'&userids='+userids,
						dataType : "json",
						success : function(data) {
							if (data['status'] == 'ERROR') {
								createAlertDialog('提示信息',alertMsg+'失败!');
							} else {
								createAlertDialog('提示信息',alertMsg+'成功!',function(){
									$("#"+dataGridID).trigger("reloadGrid");//刷新DataGrid数据
								});
							}
						},
						error : function() {
							createAlertDialog('提示信息','网络繁忙,请稍后再试!');
						}
					});
				});
			});
		});
		function getRoleInfoByTree(id){
			roleId=id;
			$.ajax({//加载角色信息
				type : "GET",
				url : '<%=request.getContextPath()%>/role.do?method=getRoleById&id='+ id,
				dataType : "json",
				success : function(data) {
						if (data['status'] == 'ERROR') {
							alert(data['msg']);
						} else {
							var tdHtml = "<td>&nbsp;</td>";
							tdHtml += "<td>" + data['roleName'] + "</td>";
							tdHtml += "<td>" + data['userName'] + "</td>";
							tdHtml += "<td>" + data['createDate'] + "</td>";
							tdHtml += "<td>" + data['roleDesc'] + "</td>";
							$("#roleInfoTr").html(tdHtml);
							getUsersByRoleId(id);
						}
				},
				error : function() {
					createAlertDialog('提示信息', '网络繁忙,请稍后再试!');
				}
			});
		}
		function getUsersByRoleId(id){
			dataGridID='roleUserDataGrid';
			offsetWidth=210;
			var path='<%=request.getContextPath()%>/role.do?method=getUsersByRoleId&id='+ id;
			var params={
				'path':path,
				'colNames':[ 'ID', '用户名', '真实姓名', '创建人', '创建时间' ],
				'colModel':[ {
					name : 'id',
					index : 'id',
					width : 10
					//百分比
					}, {
						name : 'userName',
						index : 'userName',
						width : 15
					}, {
						name : 'employeeName',
						index : 'employeeName',
						width : 15
					}, {
						name : 'createUserName',
						index : 'createUserName',
						width : 30
					}, {
						name : 'createDate',
						index : 'createDate',
						width : 30
				} ]
			};
			initJQGrid(params,dataGridID);
			$("#"+dataGridID).jqGrid('setGridParam', {
				url : path
			}).trigger("reloadGrid");//重新设置url重新加载数据

	}
</script>
<table width="100%" border='0' cellpadding='0' cellspacing='0'>
	<tr>
		<td style="padding: 4px 5px;"><a href='javascript:void(0);'
			id="newRoleBtn" url="./RoleAdd.jsp" class='zPushBtn' tabindex='-1'><img
				src="../../images/icons/icon025a2.gif" /><b>新建&nbsp;</b></a> <a
			href='javascript:void(0);' id='editRoleBtn' class='zPushBtn'
			url="<%=request.getContextPath()%>/role.do?method=updateRole"
			tabindex='-1' id=''><img src="../../images/icons/icon025a4.gif" /><b>修改&nbsp;</b></a>
			<a href='javascript:void(0);' id='delRoleBtn' class='zPushBtn'
			tabindex='-1' id=''><img src="../../images/icons/icon025a3.gif" /><b>删除&nbsp;</b></a>
			<font color='red'> 系统管理员组是系统的超级管理员组，拥有系统的所有权限</font></td>
	</tr>
	<tr>
		<td style="padding: 0px 5px;">
			<table width="100%" cellpadding="2" cellspacing="0" class="dataTable">
				<tr class="dataTableHead">
					<td width="5%">&nbsp;</td>
					<td width="15%"><b>名称</b></td>
					<td width="15%"><b>创建人</b></td>
					<td width="20%"><b>创建时间</b></td>
					<td width="45%"><b>备注</b></td>
				</tr>
				<tr id="roleInfoTr">
				</tr>
			</table>
		</td>
	</tr>
	<tr>
		<td style="padding: 4px 5px;"><a href='javascript:void(0);'
			url='./RoleTabUserList.jsp' class='zPushBtn' tabindex='-1'
			id='addUserToRoleBtn'><img src="../../images/icons/icon021a2.gif" /><b>添加用户到角色&nbsp;</b></a>
			<a href='javascript:void(0);' class='zPushBtn' tabindex='-1' id='delUserFromRoleBtn'><img
				src="../../images/icons/icon021a3.gif" /><b>从角色中删除用户&nbsp;</b></a></td>
	</tr>
	<tr>
		<td id="dataGridTd"
			style="padding-top: 0px; padding-left: 6px; padding-right: 6px; padding-bottom: 8px;">
			<table id="roleUserDataGrid" width=100% class="dataGridTable"></table>
			<div id="pager"></div>
		</td>
	</tr>
</table>